package com.securitydome05.pojo;

import lombok.Data;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import java.util.*;

@Data
public class User implements UserDetails {

  private Integer id;
  private String username;
  private String password;
  private Boolean enabled;             //账号是否激活
  private Boolean accountNonExpired;   //账号是过期
  private Boolean accountNonLocked;    //账号是否锁定
  private Boolean credentialsNonExpired; //密码是否过期
  private List<Role> roles= new ArrayList<>();   //关系属性，存储当前用户所有的角色

  @Override
  public Collection<? extends GrantedAuthority> getAuthorities() {
    //把角色转变成权限的集合
    Set<SimpleGrantedAuthority> authorities = new HashSet<>();
    roles.forEach(role -> {
      SimpleGrantedAuthority simpleGrantedAuthority = new SimpleGrantedAuthority(role.getName());
      authorities.add(simpleGrantedAuthority);
    });
    return authorities ;
  }

  @Override
  public boolean isAccountNonExpired() {
    return accountNonExpired;
  }

  @Override
  public boolean isAccountNonLocked() {
    return accountNonLocked;
  }

  @Override
  public boolean isCredentialsNonExpired() {
    return credentialsNonExpired;
  }

  @Override
  public boolean isEnabled() {
    return enabled;
  }
}
